package com.lucifer.cloud.boot.blog.service.impl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lucifer.cloud.boot.blog.domin.bo.Article;
import com.lucifer.cloud.boot.blog.domin.bo.Blog;
import com.lucifer.cloud.boot.blog.domin.type.BlogConstant;
import com.lucifer.cloud.boot.blog.config.UserSystem;
import com.lucifer.cloud.boot.blog.domin.bo.Exhibition;
import com.lucifer.cloud.boot.blog.domin.bo.Star;
import com.lucifer.cloud.boot.blog.domin.dto.star.StarConverter;
import com.lucifer.cloud.boot.blog.domin.type.BlogType;
import com.lucifer.cloud.boot.blog.mapper.ArticleMapper;
import com.lucifer.cloud.boot.blog.mapper.BlogMapper;
import com.lucifer.cloud.boot.blog.mapper.ExhibitionMapper;
import com.lucifer.cloud.boot.blog.mapper.StarMapper;
import com.lucifer.cloud.boot.blog.service.StarService;
import com.lucifer.cloud.commons.model.Result;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @author lucifer
 * @date 2023/12/21 15:24
 */
@Service
public class StarServiceImpl extends ServiceImpl<StarMapper, Star> implements StarService {

    @Resource
    private UserSystem userSystem;

    @Resource
    private BlogMapper blogMapper;
    @Resource
    private ExhibitionMapper exhibitionMapper;

    @Resource
    private ArticleMapper articleMapper;

    @Override
    public Result star(HttpServletRequest request, String uid,String createdUserId, Integer star_type, Integer type) {
        Long userId = userSystem.userId(request);
        if(Objects.isNull(userId)){
            return Result.error(HttpStatus.UNAUTHORIZED.value(),BlogConstant.NOT_LOGIN);
        }
        Star sr = getOne(Wrappers.lambdaQuery(Star.class).eq(Objects.nonNull(userId),Star::getUser_id, userId).eq(Star::getStar_id, uid));
        if (Objects.isNull(sr)){
            Star star = StarConverter.converterReq2Star(userId,uid,createdUserId, star_type, type);
            save(star);
        }else {
            update(Wrappers.lambdaUpdate(Star.class)
                    .set(Star::getUpdated_at, LocalDateTime.now())
                    .set(Star::getStar_type,star_type)
                    .eq(Objects.nonNull(userId),Star::getUser_id,userId)
                    .eq(Star::getStar_id,uid)
                    .eq(StringUtils.isNotBlank(createdUserId),Star::getCreated_user_id,createdUserId)
                    .eq(Star::getType,type));
        }

        if(Objects.equals(BlogType.BLOG.getIndex(),type)){
            blogMapper.update(new Blog(),Wrappers.lambdaUpdate(Blog.class)
                    .set(Blog::getUpdated_at,LocalDateTime.now())
                    .setSql(BlogConstant.ASC_STAR_TYPE.equals(star_type) ?  "`thumbs_up`=`thumbs_up`+1":"`thumbs_up`= if(`thumbs_up` < 1, 0, `thumbs_up`-1)")
                    .eq(Blog::getUid,uid)
            );
        }

        if(Objects.equals(BlogType.IMAGE.getIndex(),type)){
            exhibitionMapper.update(new Exhibition(),Wrappers.lambdaUpdate(Exhibition.class)
                    .set(Exhibition::getUpdated_at,LocalDateTime.now())
                    .setSql(BlogConstant.ASC_STAR_TYPE.equals(star_type) ?  "`thumbs_up`=`thumbs_up`+1":"`thumbs_up`= if(`thumbs_up` < 1, 0, `thumbs_up`-1)")
                    .eq(Exhibition::getUid,uid)
            );
        }
        if(Objects.equals(BlogType.ARTICLE.getIndex(),type)){
            articleMapper.update(new Article(),Wrappers.lambdaUpdate(Article.class)
                    .set(Article::getUpdated_at,LocalDateTime.now())
                    .setSql(BlogConstant.ASC_STAR_TYPE.equals(star_type) ?  "`thumbs_up`=`thumbs_up`+1":"`thumbs_up`= if(`thumbs_up` < 1, 0, `thumbs_up`-1)")
                    .eq(Article::getUid,uid)
            );
        }
       return Result.success(Objects.equals(BlogConstant.ASC_STAR_TYPE,star_type)?"点赞成功":"取消点赞成功",true);
    }
}
